package network;

import java.io.File;
import java.io.IOException;

public class FileSynchro extends Thread {

	Connexion con;
	File parentdir;
	FilesList archiveList;
	
	public FileSynchro(Connexion c, String path){
		
		this.con = c;
		this.parentdir = new File(path);
		
		File srepindices = new File(path+"/indices");
		File srepalertes = new File(path+"/alertes");
		
		if(!this.parentdir.isDirectory())
		{
			this.parentdir.mkdir();
		}
		
		if(!srepalertes.isDirectory()){
			srepalertes.mkdir();
		}
		
		if(!srepindices.isDirectory()){
			srepindices.mkdir();
		}
		
		this.archiveList = null;
	}
	
	public void run(){
		while(this.isAlive()){
			String fromDir = parentdir.getPath(); 
			String toDir = fromDir;
			
			FilesList currentList 	= new FilesList (fromDir);
			File[] FROM = currentList.getFiles();
			
			File[] TO = null;
			boolean arch = false;
			if(archiveList != null){
				TO = archiveList.getFiles();
				arch = true;
			}
			else {
				MyFile MFparent = null;
				try {
					MFparent = new MyFile(parentdir.getPath());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if(con.state && MFparent != null){
					if(MFparent.isFile())
					con.send(MFparent);
				}
			}
			
			// Analysing the first list of files
	    	for (int i = 0 ; i < FROM.length ; i++){
	    		int index1 = 0;
	    		String fromPath = FROM[i].getPath();
	    		String fromName = FROM[i].getName();
	    		fromPath = fromPath.substring (fromDir.length());
	    		
	    		for (int j = 0 ; arch && j < TO.length ; j++){    			
	    			String toPath = TO[j].getPath();
	    			toPath = toPath.substring (toDir.length());
	    			
	    			/* Verification de version */
	    			
	    			if (fromPath.equals(toPath)){
	    				if (!FROM[i].isDirectory() && FROM[i].lastModified() > TO[j].lastModified()){
	    					//copyFile (FROM[i], TO[j], toDir);
	    					MyFile MFfrom = null;
							try {
								MFfrom = new MyFile(FROM[i].getPath());
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
	    	    			
	    	    			//copyFile (MFfrom, newFile, dialog, toDir);
	    	    			if(con.state && MFfrom != null){
	    	    				if(MFfrom.isFile())
	    	    					con.send(MFfrom);
	    	    				String df = "File :";
	    	    				if(MFfrom.isDirectory())
	    	    					df="Dir :";
	    	    				//System.out.println(df + MFfrom.getName() + " was sended\n");
	    	    			}
	    				}
	    				index1 = 1;
	    				break;
	    			}    			
	    		}
	    
	    		if (index1 == 0 && !(fromName.startsWith("~"))){
	    			// Creates a new path
	    			String newPath = FROM[i].getPath();
	    			newPath = newPath.substring(fromDir.length());
		        	newPath = toDir + newPath;
		        	
		        	// Creates a new file with this path
	    			//File newFile = new File (newPath);
	    			
	    			// bufferize the file
	    			MyFile MFfrom = null;
					try {
						MFfrom = new MyFile(FROM[i].getPath());
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
	    			
	    			//copyFile (MFfrom, newFile, dialog, toDir);
	    			if(con.state && MFfrom != null){
	    				con.send(MFfrom);
	    				String df = "File :";
	    				if(MFfrom.isDirectory())
	    					df="Dir :";
	    				//System.out.println(df + MFfrom.getName() + " was sended\n");
	    			}
	    			
	    		}
	    	}
	    	
	    	// Analysing the second list of files	
	    	for (int l = 0 ; arch && l < TO.length ; l++){
	    		int index2 = 0;
	    		String toPath2 = TO[l].getPath();
	    		toPath2 = toPath2.substring (toDir.length());
	    		
	    		for (int k = 0 ; k < FROM.length ; k++){
	    			String fromPath2 = FROM[k].getPath();
	    			fromPath2 = fromPath2.substring (fromDir.length());
	    			if (toPath2.equals(fromPath2)){
	    				 index2 = 1;
	    				 break;
	    			}
	    		}
	    			
	    		if (index2 == 0){
	    			// Deletes the current file
	    			//TO[l].delete();
	    			
	    			// bufferize and send
	    			MyFile MFto_delete = null;
					try {
						MFto_delete = new MyFile(TO[l].getPath(),true);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
	    			if(con.state && MFto_delete != null){ 
	    				//log.addline("File '"+ TO[l].getName() + "' was deleted\n");
	    				con.send(MFto_delete);
	    				//System.out.println("File '"+ TO[l].getName() + "' was deleted\n");
	    			}
	    		}	
	    	}
	    	archiveList = currentList;
		}
	}
}
